package cn.pku.ss.fa.sessionbean;

import javax.ejb.Stateless;
import javax.persistence.*;


import java.util.*;
import cn.pku.ss.fa.bean.*;
import cn.pku.ss.fa.entity.*;

@Stateless
public class CityManager implements CityManagerLocal {

	@PersistenceContext
	protected EntityManager manager;
	
	public List<ProvinceBean> findAllProvinces() {
		System.out.println("Enter findAllProvinces");
		Query query = manager.createQuery("select a from Province a");
		List<Province> list = query.getResultList();
		List<ProvinceBean> resultList = new ArrayList<ProvinceBean>();
		for(int i = 0; i < list.size(); i++) {
			Province province = list.get(i);
			ProvinceBean provinceBean = new ProvinceBean();
			provinceBean.setId(province.getId());
			provinceBean.setProvinceName(province.getName());
			List<CityBean> cityBeanList = new ArrayList<CityBean>();
			Set<City> cities = province.getCities();
			System.out.println("citie size: " + cities.size());
			for (Iterator iter = cities.iterator(); iter.hasNext();) {
				City city = (City)iter.next();
				CityBean cityBean = new CityBean();
				cityBean.setId(city.getId());
				cityBean.setCityName(city.getName());
				cityBeanList.add(cityBean);
			}
			provinceBean.setCityNames(cityBeanList);
			resultList.add(provinceBean);
		}
		
		return resultList;
	}
}
